define(function () {
    'use strict';

    function ctrl($scope, $stateParams,$ionicLoading,CommonService, FinancingService, LocalCacheService,$sce) {
        var contents = [];
        var user = LocalCacheService.getUser();
        $scope.agreement = {
            content: '',
            state: $stateParams.typeid,
        };
        $scope.agreements = {};
        function init() {
            FinancingService.getAgreements({
                client_id: user.client_id,
                fund_account: user.fund_account
            }).then(function (data) {
                $scope.agreements=data.agreements;
                contents[0] = data.agreements[0].agreement_content;
                contents[1] = data.agreements[1].agreement_content;
                $scope.agreement.content =  $sce.trustAsHtml(contents[0]);
            });
            $scope.par = {current: 0};
            $scope.typeCLick = function (clickType) {
                $scope.par.current = clickType;
                $scope.agreement.content = $sce.trustAsHtml(contents[clickType]);
            }
        }

        $scope.$on('$ionicView.loaded', function () {
            init();
        });
        $scope.submit = function () {
            $ionicLoading.show();
            //提交开通，成功后提示
            var signValue=$scope.agreements[0].econtract_id+';'+$scope.agreements[0].econtract_md5+';sign|'+
                $scope.agreements[1].econtract_id+';'+$scope.agreements[1].econtract_md5+';sign';
            FinancingService.setAgreement({
                client_id: user.client_id,
                fund_account: user.fund_account,
                signValue:signValue
            }).then(function (data) {
                    if(data.error_info){
                        CommonService.showConfig({message:data.error_info});
                        $ionicLoading.hide();
                    }else{
                        FinancingService.addUser({
                            client_id: user.client_id,
                            fund_account: user.fund_account
                        }).then(function (data) {
                            if(data.error_no!='0'){
                                CommonService.showConfig({message:data.error_info});
                                $ionicLoading.hide();
                            }else{
                                CommonService.showComplete('../view/template/easytemplate.html', '返回首页', 'tab.account-index');
                                $ionicLoading.hide();
                            }
                        });
                    }
                }
            );
        }
    }

    ctrl.$inject = ['$scope', '$stateParams', '$ionicLoading', 'CommonService', 'FinancingService', 'LocalCacheService','$sce'];
    return ctrl;
});
